#include <bits/stdc++.h>
#define rep(i,l,r) for (int i = l; i <= r; i ++)
#define per(i,r,l) for (int i = r; i >= l; i --)
using namespace std;
int Q;
string S,T;
map<string,int>Ser;
inline bool Num (string P,int l,int r,int Cmp) {
	if (l > r) return 0;
	if (P[l] == '0' && P[l + 1] == '0') return 0;
	if (P[l] == '-') return 0;
	long long x = 0;
	rep (i,l,r) {
		x = x * 10 + (long long)(P[i] - '0');
		if (x > Cmp) {
			return 0;
		}
	}
	return 1;
}
inline bool Fnd (string P) {
	int flag = 0,flaG = 0,lst = 0;
	rep (i,0,P.size ()) if (P[i] == '.') {
		flag ++;
		if (Num (P,lst,i - 1,255)) lst = i + 1;
		else return 0;
	}
	else if (P[i] == ':') {
		flaG ++;
		if (flaG != 1 || flag != 3) return 0;
		if (! Num (P,lst,i - 1,255) && Num (P,i + 1,P.size () - 1,65535)) return 0;
	}
	return 1;
}
int main () {
    freopen ("network.in","r",stdin);
    freopen ("network.out","w",stdout);
    scanf ("%d",&Q);
    rep (i,1,Q) {
    	cin >> S >> T;
    	if (! Fnd (T)) {
    		printf ("ERR\n"); continue;
    	}
    	else {
    		if (S[0] == 'S') {
    			if (Ser[T]) {
    				printf ("FAIL\n"); continue;
    			}
    			else Ser[T] = i,printf ("OK\n");
    		}
    		else {
   				if (Ser[T]) {
    				printf ("%d\n",Ser[T]); continue;
    			}
    			else {
    				printf ("FAIL\n"); continue;
    			}
    		}
    	}
    }
	return 0;
}
